<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../../../css/common.css" media="all">

	</head>
	<body>

		<div class="layui-fluid">
			<div class="layui-row" id="view">
				<form class="layui-form" lay-filter="test_detail">
					<div class="layui-card">
						<div class="layui-card-body">
							<div class="layui-form-item">
								<label class="layui-form-label">分类名称</label>
								<div class="layui-input-block">
									<input type="text" name="name" placeholder="分类名称" autocomplete="off" class="layui-input">
								</div>
							</div> 
						</div> 
					</div>

					<div class="layui-form-item layui-layout-admin">
						<div class="layui-input-block">
							<div class="layui-footer" style="left: 0;">
								<button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>
		<script src="../../../../layuiadmin/layui/layui.js"></script>
		<script src="../../../../js/jquery.min.js"></script>
		<script src="../../../../js/jquery.jqprint-0.3.js"></script>
		<script src="../../../../js/common.js"></script>
		<script>
			var _data = {
				id: getParam('id')
			}
			// console.log(_data.id);

			layui.config({
				base: '../../../../layuiadmin/' //静态资源所在路径
			}).extend({
				index: 'lib/index' //主入口模块
			}).use(['index', 'form', 'laytpl', 'laydate', 'layedit', 'upload'], function() {
				var $ = layui.$,
					admin = layui.admin,
					upload = layui.upload,
					laytpl = layui.laytpl,
					laydate = layui.laydate,
					form = layui.form;

				var img_arr1 = [],
					img_arr2 = [];

				if (_data.id) {
					initdata();
				}

				var layedit = layui.layedit;
				layedit.build('demo_editor'); //建立编辑器

				//监听提交
				form.on('submit(demo1)', function(data) {
					var data = data.field;
					_data.goods_img = img_arr1.join(',');
					_data = Object.assign(_data, data);

					layer.confirm('确定要提交数据？', function() {
						loading();
						if (_data.type == 0) {
							_data.test_typ == '';
						}
						if (_data.id) {
							//submitdata(server.op_updateproduct);
						} else {
							submitdata(server.news_insert);
						}
					}, function() {
						layer.msg('取消操作');
					});
					return false;
				});

				function submitdata(_url) {
					$.post(_url, _data, function(res, status) {
						if (status == 'success') {
							if (res.code == 0) {
								layer.alert('操作成功', function() {
									parent.layer.closeAll();
									parent.layui.table.reload('test-table-operate');
								});
							} else {
								layer.msg(res.msg);
							}
							closeloading();
						} else {
							closeloading();
							layer.msg('服务器连接错误');
						}
					});
				}

				window.choose = function(data) {
					layer.open({
						type: 2,
						title: '上传图片',
						shadeClose: false,
						shade: 0.5,
						maxmin: true, //开启最大化最小化按钮
						area: ['700px', '400px'],
						content: '../../goods/model/up_report.html?type=' + data,
						end: function() {},
						success: function() {}
					});
				}				
				window.getimg = function(data, type) {
					if(data) {
						switch(type) {
							case '1':
								createli(data, 'zhutu', type)
								break;
							case '2':
								createli(data, 'video', type)
								break;
							default:
								break;
						}
					}
				}
				window.delimg = function(i, type, _self) {
					var _type = String(type)
					layer.confirm('确定要删除图片？', function(index) {
						switch (_type) {
							case '1':
								img_arr1.splice(i, 1)
								break;
							default:
								break;
						}
						$(_self).parent().remove();
						layer.close(index)
					}, function() {
						layer.msg('取消操作');
					});
				}

				function createli(data, dom, type) {
					var lihtml = '';
					switch (type) {
						case '1':
							if (data.indexOf(',') == -1) {
								img_arr1.push(data)
							} else {
								for (var i = 0; i < data.split(',').length; i++) {
									img_arr1.push(data.split(',')[i])
								}
							}
							for (var i = 0; i < img_arr1.length; i++) {
								lihtml += '<li class="show_img_li">' +
									'<img src=' + img_arr1[i] + ' >' +
									'<p onclick="delimg(' + i + ',1,this)">删除</p>' +
									'</li>'
							}
							break;
						case '2':
							if(data.indexOf(',') == -1) {
								img_arr2.splice(0, 1, data)
							} else {
								for(var i = 0; i < data.split(',').length; i++) {
									img_arr2.push(data.split(',')[i])
								}
							}
							for(var i = 0; i < img_arr2.length; i++) {
								lihtml += '<li class="show_img_li">' +
									'<video controls="controls" id="video{{index}}" width="320" height="240"><source src=' + img_arr2[i] + ' type="video/ogg" /><source src=' + img_arr2[i] + ' video/mp4" /></video>' +
									'<p onclick="delimg(' + i + ',1,this)">删除</p>' +
									'</li>'
							}
							break;
						default:
							break;
					}
					$('#' + dom).html(lihtml);
				}
				 
				
				/**
				 * 商品分类 ---- 根据fid获取下级目录
				 * @param {Object} fid
				 */
				var initcatdata = function(fid) {
					var data1;
					if (fid) {
						$.ajax({
							url: server.op_queryproduct_catlist,
							data: {
								fid: fid
							},
							async: false,
							type: 'get',
							success: function(res) {
								if (res.code == 0) {
									var data = res.product_category.data;
									data1 = data;
								}
							},
							error: function(res) {
								errorshow();
							}
						})
					}
					return data1;
				}
				/**
				 * 数据集 以 下拉框的形式显示
				 * @param {Object} optionData
				 * @param {Object} selectname
				 */
				var creategoodsSelect = function(optionData, selectname) {
					var html = '';
					html += '<div class="layui-input-inline">';
					html += ' <select lay-filter=' + selectname + ' lay-verify="required" name=' + selectname + '>';
					html += '  <option value="">请选择</option>';
					for (var i = 0; i < optionData.length; i++) {
						html += ' <option value=' + optionData[i].id + ' title=' + optionData[i].id + '>' + optionData[i].name +
							'</option>';
					}
					html += ' </select>';
					html += '</div>';
					return html;
				}
				var goods_item = initcatdata(1);
				/**
				 * 初始化分类   给指定的下拉框填充
				 * @param {Object} domname catid
				 * @param {Object} selected
				 */
				var initcat = function(domname, selected) {
					var selected = selected;
					var $selectWrap = $('#' + domname);

					$selectWrap.find('.layui-input-inline').remove();
					var html = creategoodsSelect(goods_item, domname);
					$selectWrap.append(html);
					if (selected) {
						var index = [];
						for (var i = 0; i < selected.length; i++) {
							var selectlist = $selectWrap.find('select>option')
							for (var j = 0; j < selectlist.length; j++) {
								var title = $(selectlist[j]).attr('title');
								if (title == selected[i]) {
									$(selectlist[j]).attr('selected', true);
								}
							}
							// 取出下级的选项值
							if (i != selected.length - 1) {
								var childItem = initcatdata(selected[i]);
								// 下级选项值存在则创建select
								if (childItem) {
									var html = creategoodsSelect(childItem, domname);
									$selectWrap.append(html);
								}
							}
						}
					}
					form.render('select');
					// 监听select
					form.on('select(' + domname + ')', function(data) {
						var $thisItem = $(data.elem).parent();
						$thisItem.nextAll('div.layui-input-inline').remove();

						var fid = data.elem[data.elem.selectedIndex].value;
						var cat_cn = data.value
						switch (domname) {
							case 'catid':
								_data.cat_id = fid
								break;
							default:
								break;
						}

						var childItem = initcatdata(fid);

						if (childItem.length > 0) {
							var html = creategoodsSelect(childItem, domname);
							$selectWrap.append(html);
							$selectWrap.find('select:last-child').attr('lay-verify', '');
							form.render('select');
						}
					});
				}
				/**
				 * 
				 */
				initcat('catid');

				var goods = new Array();
				function querygoodsdata1(fid) {
					loading();
					goods = [];
					if (fid) {
						$.ajax({
							url: server.op_queryproduct_catlist,
							data: {
								id: fid
							},
							type: 'get',
							success: function(res) {
								debugger
								if (res.code == 0) {
									var data = res.product_category.data;
									/*var str = '1.35.49.24.1.1.';*/
									goods.unshift(data[0])
									var str = data[0].sid
									var sid = data[0].sid;
									/*var sid = str.substr(0, str.lastIndexOf('.', str.lastIndexOf('.') - 1) + 1);*/
									querygoodsdata2(sid)
									closeloading();
								}
							},
							error: function(res) {
								errorshow();
								closeloading();
							}
						})
					}
				}

				function querygoodsdata2(sid) {
					if (sid) {
						$.ajax({
							url: server.op_queryproduct_catlist,
							data: {
								sid: sid
							},
							type: 'get',
							success: function(res) {
								if (res.code == 0) {
									var data = res.product_category.data;
									if (goods[0].id != data[0].id) {
										goods.unshift(data[0])
									}
									
									console.log(data[0].fid != 1)
									
									if (data[0].fid != 1) {
										var sid = data[0].sid.substr(0, data[0].sid.lastIndexOf('.', data[0].sid.lastIndexOf('.') - 1) + 1);
										/*var sid = data[0].sid;*/
										querygoodsdata2(sid)
									} else {
										var selected1 = [];
										for (var i = 0; i < goods.length; i++) {
											selected1.push(goods[i].id)
										}
										initcat('catid', selected1);
										closeloading()
									}
								}
							},
							error: function(res) {
								closeloading();
								errorshow();
							}
						})
					}
				}

				function initdata() {
					console.log(_data)
					$.get(server.op_queryproductdetail, _data, function(res, status) {
						if (status == 'success') {
							if (res.code == 0) {
								var data = res.product
								if ($("select[name=catname] option").length > 0) {
									$("select[name=catname]").val(data.data.catid);
								}
								data.data.goods_img ? createli(data.data.goods_img, 'zhutu', '1') : '',
								
								console.log(data.data.catid)
								querygoodsdata1(data.data.catid, 'catid')
								form.val("test_detail", data.data);
								form.render();
								form.render('select');
							} else {
								layer.msg(res.msg);
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}
			});
		</script>
	</body>
</html>
